//
//  ViewController.m
//  DemoTree
//
//  Created by 尤维维 on 2017/11/10.
//  Copyright © 2017年 EmicNet. All rights reserved.
//

#import "ViewController.h"
#import "TreeView.h"

@interface ViewController ()<TreeViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 获取层级数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"company" ofType:@"plist"];
    NSArray *items = [self dictToModel:[NSArray arrayWithContentsOfFile:path]];
    
    // 构件TreeView
    TreeView *testView = [[TreeView alloc] initWithFrame:self.view.bounds delegate:self];
    [self.view addSubview:testView];
    testView.items = items;
    
}

- (NSArray *)dictToModel:(NSArray *)array {
    NSMutableArray *resultArray = [NSMutableArray array];
    for (NSDictionary *company in array) {
        if (company[@"level"]) {
            TreeNodeModel *model = [TreeNodeModel new];
            model.name = company[@"name"];
            model.level = [company[@"level"] intValue];
            if (company[@"items"]) {
                model.items = [self dictToModel:company[@"items"]];
            }
            [resultArray addObject:model];
        }else{
            [resultArray addObject:company];
        }
    }
    return [NSArray arrayWithArray:resultArray];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableViewCellHeight {
    return 44;
}

- (CGFloat)tableViewHeadHeight {
    return 35;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellOfTheIndexPathModel:(NSDictionary *)model {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellID"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setSeparatorInset:UIEdgeInsetsMake(0, -15, 0, 0)];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"\t%@",model[@"name"]];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",model[@"phone"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath model:(id)model {
    NSLog(@"%@",model);
}


@end
